package dao;

import bean.Project;
import bean.Worker;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.query.Query;

import java.util.List;

import javax.persistence.criteria.CriteriaQuery;

public class WorkerDAOImpl implements WorkerDAO {
    private Session session;
    private Transaction transaction;
    private boolean completed;

    public WorkerDAOImpl(Session session) {
        this.session = session;
    }

    @Override
    public boolean add(Worker worker) {
        try {
            transaction = session.beginTransaction();
            Project project = session.get(Project.class, worker.getProjectId());
            project.getWorkers().add(worker);
            session.update(project);
            completed = true;
        } catch (Exception e) {
            completed = false;
            e.printStackTrace();
        } finally {
            transaction.commit();
        }
        return completed;
    }

    @Override
    public boolean edit(Worker temporary) {
        try {
            transaction = session.beginTransaction();
            Worker worker = session.get(Worker.class, temporary.getWorkerId());
            Project project = session.get(Project.class, worker.getProjectId());
            project.getWorkers().remove(worker);
            session.update(project);
            project = session.get(Project.class, temporary.getProjectId());
            worker.setWorkerName(temporary.getWorkerName());
            worker.setSex(temporary.getSex());
            worker.setTelephone(temporary.getTelephone());
            worker.setProjectId(temporary.getProjectId());
            project.getWorkers().add(worker);
            session.update(project);
            completed = true;
        } catch (Exception e) {
            completed = false;
            e.printStackTrace();
        } finally {
            transaction.commit();
        }
        return completed;
    }

    @Override
    public boolean remove(int workerId) {
        try {
            transaction = session.beginTransaction();
            Worker worker = session.get(Worker.class, workerId);
            session.delete(worker);
            completed = true;
        } catch (Exception e) {
            completed = false;
            e.printStackTrace();
        } finally {
            transaction.commit();
        }
        return completed;
    }

    @Override
    public List<Worker> queryAll() {
        List<Worker> workers = null;
        try {
//            Query<Worker> query = session.createQuery("SELECT worker FROM Worker AS worker");
//            workers = query.list();
            CriteriaQuery<Worker> cq = session.getCriteriaBuilder().createQuery(Worker.class);
            // 获取根实体
            cq.from(Worker.class);
            workers = session.createQuery(cq).list();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return workers;
    }

    @Override
    public List<Worker> queryByWorkerName(String keyword) {
        List<Worker> workers = null;
        try {
            @SuppressWarnings("unchecked")
            Query<Worker> query = session.createQuery("SELECT new Worker(workerId,workerName,sex,telephone,projectId) FROM Worker WHERE workerName LIKE :workerName");
            query.setParameter("workerName", "%" + keyword + "%");
            workers = query.list();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return workers;
    }

    @Override
    public List<Worker> queryByTelephone(String keyword) {
        List<Worker> workers = null;
        try {
            @SuppressWarnings("unchecked")
            Query<Worker> query = session.createQuery("SELECT worker FROM Worker worker WHERE worker.telephone LIKE :telephone");
            query.setParameter("telephone", "%" + keyword + "%");
            workers = query.list();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return workers;
    }

    @Override
    public List<Worker> queryByWorkerId(int workerId) {
        List<Worker> workers = null;
        try {
            CriteriaQuery<Worker> cq = session.getCriteriaBuilder().createQuery(Worker.class);
            // 获取根实体
            cq.from(Worker.class);
            workers = session.createQuery("FROM Worker worker WHERE worker.workerId=" + workerId, Worker.class).list();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return workers;
    }
}
